1 00:00:00,230 --> 00:00:01,220 Welcome back. 2 00:00:01,220 --> 00:00:05,450 We're virtually finished with our project, but there's just one last thing that I'd like for us to 3 00:00:05,450 --> 00:00:06,500 fix in our SCP. 4 00:00:06,740 --> 00:00:12,650 Now, if you remember, in our server side drink class, when we set the owner for a particular drink 5 00:00:12,650 --> 00:00:19,820 right here, we're connecting a function to when the player dies and when that player dies, we go ahead 6 00:00:19,820 --> 00:00:23,510 and go and destroy any drink objects that are in their inventory. 7 00:00:23,540 --> 00:00:29,840 Now, there is a very slight problem with this system, and that's because we're not disconnecting this 8 00:00:29,840 --> 00:00:34,160 event on our character for drinks that may have been destroyed already. 9 00:00:34,670 --> 00:00:38,180 So I'm going to show you a very good example of this problem. 10 00:00:38,330 --> 00:00:44,660 Let's say I go to my drink machine and I go and get a cup of air. 11 00:00:48,970 --> 00:00:51,640 And let's say I consume it and it gets destroyed. 12 00:00:51,640 --> 00:00:55,390 And let's go ahead and say I get another drink, maybe a cup of pain. 13 00:00:58,280 --> 00:01:02,270 And I go ahead and drink it, and that kind of fun stuff happens. 14 00:01:02,270 --> 00:01:07,520 Now, if I go and reset my character, you're going to see an error pop up in the console and you're 15 00:01:07,520 --> 00:01:12,410 going to see it right here in our server drink class, attempt to call Myscene method destroy of table. 16 00:01:12,410 --> 00:01:16,880 And if we trace this back to our drink class, we're getting our error message right here. 17 00:01:16,880 --> 00:01:19,820 And it's inside of our on owner death function. 18 00:01:19,820 --> 00:01:26,210 And that's because our error cup got destroyed previously, but we never disconnected the function attached 19 00:01:26,210 --> 00:01:28,280 to the died event on our character. 20 00:01:28,370 --> 00:01:30,650 So that's a problem we need to fix. 21 00:01:30,830 --> 00:01:35,030 And to fix that I'm going to create another table inside of our server drink class. 22 00:01:35,030 --> 00:01:36,770 I'm going to call it connections. 23 00:01:36,770 --> 00:01:43,160 And this is going to store a table full of all of the connections for specific drinks in our game. 24 00:01:43,160 --> 00:01:47,930 So let's go ahead and go down to the set owner function here. 25 00:01:48,080 --> 00:01:55,610 And what I want to do is I want to check if there is not a table that exists for this particular drink 26 00:01:55,610 --> 00:01:57,230 inside of our connections table. 27 00:01:57,230 --> 00:02:04,130 So if there is not in our connections table, a key value pair for this particular drink with this ID, 28 00:02:04,160 --> 00:02:05,870 then we need to create one. 29 00:02:05,870 --> 00:02:10,520 So inside of our connections we're going to create a new key value pair using the ID of this drink. 30 00:02:10,520 --> 00:02:12,680 And it's going to be set to an empty table. 31 00:02:12,680 --> 00:02:18,740 And then inside of this table we can define some key value pairs that are going to store some events 32 00:02:18,740 --> 00:02:20,150 or connections. 33 00:02:20,150 --> 00:02:26,240 So right here when we connect this function to this event it's going to return back to us I believe 34 00:02:26,240 --> 00:02:28,340 it's called an RRB script connection. 35 00:02:28,340 --> 00:02:33,230 And we need to store that inside of our connections table so we can do connections. 36 00:02:33,230 --> 00:02:35,570 Access the self.id table. 37 00:02:35,570 --> 00:02:38,090 And inside of this table we'll create a new key value pair. 38 00:02:38,090 --> 00:02:41,360 I'm just going to call it Owner Death. 39 00:02:41,960 --> 00:02:46,700 And it's going to be equal to the R script connection that gets returned from this connect function. 40 00:02:46,850 --> 00:02:54,500 So now that means when this drink object gets destroyed, we need to disconnect any possible connections 41 00:02:54,500 --> 00:02:57,140 that this particular drink with this ID had. 42 00:02:57,170 --> 00:03:00,920 So let's go ahead and go to our destroy function. 43 00:03:00,920 --> 00:03:02,390 So destroy drink object. 44 00:03:02,390 --> 00:03:08,420 What we need to do here is we need to loop through our connections table. 45 00:03:08,720 --> 00:03:16,700 So what I could do is I could check if connections has a table for this particular drink with this ID. 46 00:03:16,880 --> 00:03:23,390 If it does, then what we could do is we could loop through every single connection in pairs. 47 00:03:23,390 --> 00:03:25,130 Connections. 48 00:03:25,400 --> 00:03:26,810 self.id. 49 00:03:28,450 --> 00:03:34,420 So we're looping through all of the key value pairs and make sure not to use I pairs, but pairs. 50 00:03:34,420 --> 00:03:37,810 And what we could do with this connection is we could disconnect it. 51 00:03:38,450 --> 00:03:44,630 And to make this more clear, I can denote this right here using type annotation as an RGB script connection 52 00:03:44,630 --> 00:03:44,960 here. 53 00:03:44,960 --> 00:03:48,590 So it's a connection between an RGB script signal and a function. 54 00:03:48,590 --> 00:03:50,120 And we disconnect it. 55 00:03:50,180 --> 00:03:58,040 And then once we've disconnected all of those functions then we can set the connections self.id equal 56 00:03:58,040 --> 00:03:58,400 to nil. 57 00:03:58,400 --> 00:04:01,850 So we're destroying this table at this key value pair. 58 00:04:01,850 --> 00:04:04,550 So that means our problem should be resolved. 59 00:04:04,550 --> 00:04:05,840 So if I go ahead. 60 00:04:07,580 --> 00:04:10,070 And get a air drink. 61 00:04:13,800 --> 00:04:18,450 And we go ahead and drink it and the cup gets destroyed when I go and reset, we shouldn't have that 62 00:04:18,450 --> 00:04:19,920 error in the console anymore. 63 00:04:19,920 --> 00:04:21,090 And there we go. 64 00:04:21,090 --> 00:04:22,740 Our error is gone. 65 00:04:22,770 --> 00:04:25,890 Other than that, our project is completed. 66 00:04:25,920 --> 00:04:31,170 I've attached the completed SCP model to the lecture, including one that contains a lot of fun drinks 67 00:04:31,170 --> 00:04:32,730 if you'd like to mess around with it. 68 00:04:33,180 --> 00:04:37,200 Otherwise, congratulations finishing this project and I'll see you in the next lecture.